const { toUrl,globalData } = getApp();
import { request } from "@/utils/request";
Page({
  toUrl,
  data: {
    globalData,
    menu: 1,
    list: [],
    len: 8,
    background: "",
    raffle: null,
    trans: 'transform: rotate(0deg);',
    deg: 0,
    clickTag: false, //true表示当前正在抽奖
    page: 1,
    logList: [],
    raffleInfo: null,
    userInfo:{},
    autoplay: false,
    duration: 2000,
    circular: true,
    imgbaseUrl:''
  },
  menu(e) {
    let {index} = e.currentTarget.dataset
    this.setData({
      menu: index,
    });
  },
  //获取积分抽奖内容
  getRaffle() {
    const that = this
    return request('raffle/index').then(res => {
      that.setData({
        raffle: res.data.raffle,
        list: res.data.raffle_goods,
      })
      if(res.data.raffle_goods.length == that.data.len) {
        return
      }else {
        that.setData({
          len: res.data.raffle_goods.length == that.data.len ? that.data.len : res.data.raffle_goods.length
        })
      }
    })
  },
  //点击抽奖
  clickRaffle() {

    if(this.data.clickTag) {
      return
    }
    this.setData({
      clickTag: true
    })
    setTimeout(() => {
      this.setData({
        clickTag: false
      })
    },5000)
    // this.getRaffle().then(() => {
      this.subRaffle().then(() => {
        const deg = this.getTransDeg()
        console.log(deg - 360 * 3)
        const degStr = `transform: rotate(${deg}deg);transition: all ease-in-out 5s;`
        this.setData({
          trans: degStr
        })
        setTimeout(() => {
          this.setData({
            trans: `transform: rotate(${deg - 360 * 3}deg);`
          })
          // this.subRaffle()
          wx.showToast({
            title: this.data.raffleInfo.message,
          })
        },5000)
      })
    // })
  },
  //获取角度
  getTransDeg() {
    const raffleId = this.data.raffleInfo.data.raffle_goods_id
    const index = this.data.list.findIndex(item => {
      return item.raffle_goods_id == raffleId
    })
    const deg = (((this.data.list.length - 1) - index) * (360 / this.data.list.length)) + 360 * 3
    console.log(((this.data.list.length - 1) - index) * (360 / this.data.list.length));
    return deg
  },
  //提交抽奖
  subRaffle() {
    const that = this
    const data = {
      raffle_id: this.data.raffle.raffle_id
    }
    return request('raffle/raffle',{data}).then(res => {
      if(res.errcode == 1){
        that.setData({
          raffleInfo: res,
          'userInfo.integral':res.data.integral
        })
        if(res.data.is_lottery == 1) {
          that.setData({
            page:1
          })
          this.myGetLoglist()
        }
      }
    }).catch(err => {
      wx.showToast({
        icon:'error',
        title: res.message,
      })
    })
  },
  //获取中奖名单
  getLoglist() {
    const that = this
    const data = {
      raffle_id: this.data.raffle.raffle_id,
      page: this.data.page
    }
    request('raffle/loglist',{data}).then(res => {
      const list = res.data
      if(list.length < 3) {
        const restLen = 3 - list.length
        for(let i = 0; i < restLen; ++i) {
          list.push([])
        }
      }
      that.setData({
        logList: list
      })
    })
  },
  onLoad(options) {
    let background = "background: conic-gradient(";
    const colors = ["#FFF6EF", "#EEF0FF"];
    //获取抽奖信息，初始化奖盘
    this.getRaffle().then(() => {
      const pro = 1 / this.data.list.length;
      this.data.list.map((item, index) => {
        //制作背景色
        let color = colors[index % 2];
        let turn1 = pro * index;
        let turn2 = pro * (index + 1);
        background +=
          (index == 0 ? "" : ",") +
          `${color} ${turn1}turn, ${color} ${turn2}turn`;
      });
      background += ")";
      this.setData({
        background,
        userInfo:getApp().globalData.baseInfo
      });
      // this.getLoglist()
      this.myGetLoglist()//我的中奖记录
    })
  },
  myGetLoglist(){
    //我的中奖记录
    const that = this
    const data = {
      page: this.data.page
    }
    request('raffle/rafflelog',{data}).then(res => {
      const list = res.data
      if(list.length < 3) {
        const restLen = 3 - list.length
        for(let i = 0; i < restLen; ++i) {
          list.push([])
        }
      }

      that.setData({
        logList: this.data.logList.concat(list)
      })
      console.log(that.data.logList);
          if (res.count > 1 && this.data.page <= res.count) {
        this.data.page++
        that.myGetLoglist()
      }

      
    })
  },
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      imgbaseUrl: getApp().globalData.baseUrl + "static/images/me25.png",
    })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
